package com.zx.tradeclient.ctrl.index5;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.widget.CommentDialog;
import com.zx.base.widget.ExpanderDialog;
import com.zx.base.widget.ExpanderDialog.DialogListener;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public class CompanyDetailInfo {

	private int page;
	private ExpanderDialog expander;
	private CommentDialog comment;
	private Context context;
	private TradeApp app;
	private LinearLayout company_info_line_cartage, company_info_line_site, company_info_line_companyIntro;
	private TextView company_info_edit_bysector, company_info_edit_city, company_info_edit_companyIntro;
	private EditText company_info_edit_companyName,company_info_edit_address, company_info_edit_email, company_info_edit_mobilephone, company_info_edit_telphone, company_info_edit_linkman;
	private String cityId="", bySector="";
	private ProgressDialog progressDialog;

	public CompanyDetailInfo(final Context context, View view) {
		this.context = context;
		app = (TradeApp) context.getApplicationContext();

		company_info_line_cartage = (LinearLayout) view.findViewById(R.id.company_info_line_cartage);
		company_info_line_cartage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				expander = new ExpanderDialog(context, new DialogListener() {

					@Override
					public void endme(String group, String value, int groupid, int valueid) {
						//type id
						company_info_edit_bysector.setText(group + " " + value);
						bySector = app.typeids[groupid][valueid];
					}
				}, 2);
				expander.show();

			}
		});
		company_info_line_site = (LinearLayout) view.findViewById(R.id.company_info_line_site);
		company_info_line_site.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 选择所在地区
				expander = new ExpanderDialog(context, new DialogListener() {

					@Override
					public void endme(String group, String value, int groupid, int valueid) {
						company_info_edit_city.setText(group + " " + value);
						cityId = app.cityids[groupid][valueid];
						
					}
				}, 1);
				expander.show();
			}
		});

		company_info_line_companyIntro = (LinearLayout) view.findViewById(R.id.company_info_line_companyIntro);
		company_info_line_companyIntro.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 公司简介
				comment = new CommentDialog((BaseActivity) context, "公司简介", "提交", "取消", false);

				comment.show();
				comment.editor.setText(company_info_edit_companyIntro.getText().toString().trim());
				comment.bt_yes.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 提交评论
						String poll_con = comment.editor.getText().toString().trim();
						company_info_edit_companyIntro.setText(poll_con);
						comment.dlg.dismiss();
					}
				});
				comment.bt_no.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 取消
						comment.dlg.dismiss();
					}
				});

			}
		});

		company_info_edit_bysector = (TextView) view.findViewById(R.id.company_info_edit_bysector);
		company_info_edit_city = (TextView) view.findViewById(R.id.company_info_edit_city);
		company_info_edit_companyIntro = (TextView) view.findViewById(R.id.company_info_edit_companyIntro);

		company_info_edit_companyName=(EditText) view.findViewById(R.id.company_info_edit_companyName);
		company_info_edit_address = (EditText) view.findViewById(R.id.company_info_edit_address);
		company_info_edit_email = (EditText) view.findViewById(R.id.company_info_edit_email);
		company_info_edit_mobilephone = (EditText) view.findViewById(R.id.company_info_edit_mobilephone);
		company_info_edit_telphone = (EditText) view.findViewById(R.id.company_info_edit_telphone);
		company_info_edit_linkman = (EditText) view.findViewById(R.id.company_info_edit_linkman);
		init();
		progressDialog = ProgressDialog.show(context, null, "正在获取数据，请稍后.....", true, true);
		readCompanyInfoTask();
	}

	public void init() {
		((BaseActivity) context).header_right.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				progressDialog = ProgressDialog.show(context, null, "正在提交数据，请稍后.....", true, true);
				editCompanyInfoTask();
			}
		});
	}

	private void editCompanyInfoTask() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "companyModify");
		map.put("id", app.user.getId());
		map.put("name",company_info_edit_companyName.getText().toString().trim());
		map.put("bysector", company_info_edit_bysector.getText().toString().trim());
		map.put("telphone", company_info_edit_telphone.getText().toString().trim());
		map.put("mobilephone", company_info_edit_mobilephone.getText().toString().trim());
		map.put("cityId", cityId);
		map.put("email",company_info_edit_email.getText().toString().trim());
		map.put("bySectorMinId", bySector);
		map.put("address", company_info_edit_address.getText().toString().trim());
		map.put("companyIntro", company_info_edit_companyIntro.getText().toString().trim());
		map.put("linkman", company_info_edit_linkman.getText().toString().trim());
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				try {
					String succeed = response.getString("succeed");
					if (succeed.equals("000")) {
						Toast toast = Toast.makeText(context, "数据提交成功。", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
						app.user.setCompany(company_info_edit_companyName.getText().toString().trim());
					} else if (succeed.equals("002")) {
						Toast toast = Toast.makeText(context, "后台出错,请联系系统管理员。", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					} else {
						Toast toast = Toast.makeText(context, "网络超时。", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} finally {
					if (progressDialog != null)
						progressDialog.dismiss();
					progressDialog = null;
				}

			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(context, "网络超时。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		});
	}

	private void readCompanyInfoTask() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "companyDetail");
		map.put("id", app.user.getId());
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseCompanyInfoJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(context, "网络超时。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}

			private void parseCompanyInfoJson(JSONObject response) {
				try {
					String succeed = response.getString("succeed");
					if (succeed.equals("000")) {
						company_info_edit_companyName.setText(response.getString("name"));
						company_info_edit_city.setText(response.getString("province")+response.getString("city"));
						company_info_edit_bysector.setText(response.getString("bySectorMax")+response.getString("bySector"));
						company_info_edit_companyIntro.setText(response.getString("companyIntro"));
						company_info_edit_address.setText(response.getString("address"));
						company_info_edit_email.setText(response.getString("email"));
						company_info_edit_mobilephone.setText(response.getString("mobilephone"));
						company_info_edit_telphone.setText(response.getString("telphone"));
						company_info_edit_linkman.setText(response.getString("linkman"));
						cityId = response.getString("cityId");
						bySector = response.getString("bySectorMinId");
						if(bySector.equals("")){
							bySector=response.getString("bySectorMaxId");
						}
					} else if (succeed.equals("002")) {
						Toast toast = Toast.makeText(context, "数据格式有误。", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					} else {
						Toast toast = Toast.makeText(context, "后台出错,请联系系统管理员。", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} finally {
					if (progressDialog != null)
						progressDialog.dismiss();
					progressDialog = null;
				}
			}
		});

	}

}
